﻿var refreshGirdData; // 更新数据
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGird();
            page.bind();
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({ keyword: keyword });
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;//新增前请清空已选中行
                learun.layerForm({
                    id: 'form',
                    title: '添加联系人',
                    url: top.$.rootUrl + '/LR_CRMModule/CustomerContact/ContactForm?customerId=',
                    width: 600,
                    height: 350,
                    maxmin: true,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                selectedRow = $('#girdtable').jfGridGet('rowdata');
                var keyValue = $('#girdtable').jfGridValue('F_CustomerContactId');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑联系人',
                        url: top.$.rootUrl + '/LR_CRMModule/Customer/Form',
                        width: 600,
                        height: 350,
                        maxmin: true,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#girdtable').jfGridValue('F_CustomerContactId');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认删除该项！', function (res) {
                        if (res) {
                            learun.deleteForm(top.$.rootUrl + '/LR_CRMModule/CustomerContact/DeleteForm', { keyValue: keyValue }, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
        },
        initGird: function () {
            $('#girdtable').jfGrid({
                url: top.$.rootUrl + '/LR_CRMModule/CustomerContact/GetPageListJson',
                headData: [
                    { label: '主键', name: 'F_CustomerContactId', hidden: true },
                    { label: '联系人', name: 'F_Contact', index: 'Contact', width: 100, align: 'left', sortable: true },
                    { label: '手机', name: 'F_Mobile', index: 'Mobile', width: 120, align: 'left', sortable: true },
                    { label: '电话', name: 'F_Tel', index: 'Tel', width: 120, align: 'left', sortable: true },
                    { label: 'QQ', name: 'F_QQ', index: 'QQ', width: 120, align: 'left', sortable: true },
                    { label: '微信', name: 'F_Wechat', index: 'Wechat', width: 120, align: 'left', sortable: true },
                    { label: '职位', name: 'F_PostId', index: 'PostId', width: 100, align: 'left', sortable: true },
                    { label: '备注', name: 'F_Description', index: 'Description', width: 200, align: 'left', sortable: true }
                ],
                mainId: 'F_CustomerContactId',
                reloadSelected: true,
                isPage: true,
                sidx: 'F_CreateDate'
            });
            page.search();
        },
        search: function (param) {
            $('#girdtable').jfGridSet('reload', { param: param });
        }
    };
    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }
    page.init();
}